
<script>
    var form;
    layui.use(['form'], function() {
        form = layui.form;

        form.on('checkbox(selone)', function(data){
            var selalllen = getselall();

            $('#onselnum').text(selalllen.length)
            if(selalllen.length>=1){
                $('.del').show()
                $('.setstatus').show()
                $('#onsel').show()
            }else{
                $('#onsel').hide()
                $('.del').hide()
                $('.setstatus').hide()


            }
        });

        $(document).on("change", '.number', function () {
            var num = parseInt($(this).val())
            var _this = $(this)
            var check = $(this).data("check")
            var dbname = $('[name="dbname"]').val()
            if(dbname=="tv"){
                var fan_cat = $('[name="fan_cat"]').val()
                if(fan_cat==4 || fan_cat==5){
                    if(isNaN(num) || num<=0){
                        layer.msg("数据输入无效，请重新输入")
                        _this.val(null)
                        return false
                    }
                    if(fan_cat == 4){
                        getmovie();
                        return false;
                    }
                }
            }
            if(isNaN(num) || num<=0){

                var zc = $('[name="zc"]').val()
                if(check){
                    //验证格式
                    let reg = /^\d+([a-zA-Z\-]|Ⅰ|Ⅱ|Ⅲ|Ⅳ|Ⅴ|Ⅵ|Ⅶ|Ⅷ|Ⅸ|Ⅹ|Ⅺ|Ⅻ)+$/g
                    let str = reg.test($(this).val())
                    if(!str){
                        layer.msg("输入内容格式错误")
                        _this.val(null)
                    }else{
                        if(zc==1){
                            layer.confirm('输入的数据不是纯数字,是否继续？', function(index){
                                //do something

                                torequest(_this)
                                layer.close(index);
                            },function () {
                                _this.val(null)
                            });
                        }else{
                            var formData = {}
                            torequest(_this)
                        }
                    }



                }else{
                    // $(this).val(null)
                }

            }else{

                if(check==1){
                    let regNum = /^\d+/
                    let isnum = regNum.test($(this).val())
                    if(!isnum){
                        let reg = /^\d+([a-zA-Z\-]|Ⅰ|Ⅱ|Ⅲ|Ⅳ|Ⅴ|Ⅵ|Ⅶ|Ⅷ|Ⅸ|Ⅹ|Ⅺ|Ⅻ)+$/g
                        let str = reg.test($(this).val())
                        layer.msg("输入内容格式错误")
                        _this.val(null)
                        return false;
                    }
                    //查看有没有重复的
                    if(zc==2 || zc==3){
                        var formData = {}
                        if(zc==2){
                            layer.confirm('输入的数据为纯数字,是否继续？', function(index){
                                //do something
                                torequest(_this,1)
                                $('.zctext').text("正常")
                                $('[name="zc"]').val(1)
                                formSetVal(formData)
                                layer.close(index);
                            },function () {

                                _this.val(null)
                            });
                        }else{
                            torequest(_this,4,true)
                        }
                    }else{
                        torequest(_this,4,true)
                    }


                }else{

                }

            }

        })
        var tipsIndex
        $('.tips').hover(function () {
            var id = $(this).data("id")
            var content = $(this).data("content")

            tipsIndex = layer.tips(content, '#tips_'+id);
        },function () {
            layer.close(tipsIndex)
        })
    })
    function torequest(_this,make_cat,cheknow) {
        var dbname = $('[name="dbname"]').val()
        var id = $('[name="id"]').val()
        var work_id = $('[name="work_id"]').val()
        var fieldName = _this.attr("name")
        var premise = _this.data('premise')
        var premiseAttr = premise.split(',')
        var field={}
        var url
        if(make_cat==null || make_cat==''){
            make_cat = 4
        }
        if(cheknow==null || cheknow==''){
            cheknow = false
        }
        switch (dbname){
            case "tv":
                url = '{:url("tv/post")}'
                break
            case "comics":
                url = '{:url("comics/post")}'
                break;
        }
        if(url){
            field[`id`] = id

            field[`work_id`] = work_id
            field[`fieldName`] = fieldName
            field[`fieldValue`] = _this.val()
            for(var i =0;i<premiseAttr.length;i++){
                field[premiseAttr[i]] = $(`[name="${premiseAttr[i]}"]`).val()
            }
            request("GET",url,field,function (res,msg) {
                //发异步，把数据提交给php

                let len = Object.keys(res)

                if(len.length>=1 && cheknow){

                    layer.confirm('已录入该话数据,是否继续？', function(index){
                        //do something
                        switchdbname(dbname,res,make_cat)

                        layer.close(index);
                    },function () {

                        _this.val(null)
                    });
                }else{
                    switchdbname(dbname,res,make_cat)
                }
            })
        }
    }
    function switchdbname(dbname,res,make_cat) {
        var formData = {}
        switch (dbname){
            case "tv":

                let len = Object.keys(res)

                if(len.length>=1){
                    formData[`title`] = res.title
                    formData[`title_jp`] = res.title_jp
                    formData[`comics_words_file1`] = res.comics_words_file1
                    formData[`comics_rolls_1`] = res.comics_rolls_1
                    formData[`comics_rolls_1_file`] = res.comics_rolls_1_file
                    if(res.comics_words_file2){
                        $('.comics_words_file2').show()
                        $('#comics_words_file').text("-")
                        $('#comics_words_file').removeClass("add")
                        $('#comics_words_file').addClass("sub")

                        formData[`comics_words_file2`] = res.comics_words_file2
                    }
                    if(res.comics_rolls_2){
                        $('.comics_rolls_2').show()
                        $('#comics_rolls').text("-")
                        $('#comics_rolls').removeClass("add")
                        $('#comics_rolls').addClass("sub")
                        formData[`comics_rolls_2`] = res.comics_rolls_2
                        formData[`comics_rolls_2_file`] = res.comics_rolls_2_file
                    }
                    formData[`zcsel`] = 2
                    formData[`zc`] = 2
                    $('.zctext').text("特殊Ⅰ")
                    setstaff(res.staff);
                    setseiyuu(res.seiyuulists);
                    formData[`make_cat`] = make_cat
                    formSetVal(formData)
                    if(make_cat==4){
                        $('.replay_make_cat_input').show()
                    }else{
                        $('.replay_make_cat_input').hide()
                    }
                }

                break
        }

    }
    function request(method,url,data,Callback,errCallback) {
        if(method=="post" || method=="POST"){
            data = JSON.stringify(data)
        }
        layer.load(2)
        $.ajax({
            url:url,
            type:method,
            contentType: "application/json", //必须有
            data:data,
            dataType:"JSON",
            success:function (res) {
                layer.closeAll()

                if(res.code==0){
                    if(res.url){
                        window.location.href=res.url
                    }else{
                        layer.msg(res.message,{icon:2})
                        if(typeof errCallback =="function"){
                            errCallback()
                        }
                    }

                }else{
                    Callback(res.data,res.message)
                }
            },error:function () {
                layer.closeAll()

            }
        })
    }
    function getselall(){
        let ids = []
        $('[name="selone"]').each(function () {
            if($(this).is(":checked")){
                ids.push($(this).val())
            }
        })
        return ids;
    }
    function sel() {
        let type = $('.selobj').data("type")
        $('[name="selone"]').prop("checked",false)
        form.render('checkbox');
        //如果是选择
        if(type==="sel"){
            $('.selobj').data("type","nosel")
            $('.selobj').text("取消选择")
            $('.editbtn').hide()
            $('.selone').show()
            $('.addline').hide()
            $('.del').hide()
        }else{
            $('.selobj').data("type","sel")
            $('.selobj').text("选择对象")
            $('.editbtn').show()
            $('.selone').hide()
            $('.addline').show()
            $('.del').show()
            $('.setstatus').hide()

        }
    }

    /**
     * @title 设置表单内容
     *
     * @param datas
     */
    function formSetVal(datas) {
        $.each(datas,function (key,value) {
            var formField = $(`[name="${key}"]`)
            if($.type(formField[0]) === 'undefined'){
                //这里面写没有表单时候的处理事件
            }else{
                var fieldTagName = formField[0].tagName.toLowerCase()
                if(fieldTagName == "input"){
                    if(formField.attr("type")=="radio"){
                        $(`input:radio[name="${key}"][value='${value}']`).attr("checked","checked")
                    }else{
                        formField.val(value)
                    }
                }else if(fieldTagName=="select"){
                    formField.val(value)
                }
            }
            form.render();
        })
    }
</script>